<?php

namespace models\Business;


use models\Exception\Business\AlbumsException;
use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;
use models\Exception\Service\ServiceException;
use models\Service\AlbumsService;
use Yaf\Exception;
use Yaf\Registry;
use youliPhpLib\Common\StringOperation;

class Album
{
    static public function getAlbumByModule($module_info = [])
    {
        try {
            $album_id_arr = [$module_info['album_id']];
            $album_id_arr = array_unique($album_id_arr);

            if (count($album_id_arr) == 1)
                $user_list['data'][] = AlbumsService::getInstance()->getOne($album_id_arr[0]);
            else
                $user_list = AlbumsService::getInstance()->getList(1, count($album_id_arr), 'desc', 'id',
                    ['groupOp' => 'AND', 'rules' => [
                        ['field' => 'id', 'op' => 'eq', 'data' => implode(',', $album_id_arr)]]]);

            $album_id_array = array_column($user_list['data'], 'album', 'id');

            if (isset($album_id_array[$module_info['album_id']]) &&
                StringOperation::isSerialized($album_id_array[$module_info['album_id']])) {
                $album_arr = unserialize($album_id_array[$module_info['album_id']]);
                foreach ($album_arr as $index => $album_url)
                    $album_arr[$index] = Registry::get('config')['application']['host'] . $album_url;

                $module_info['album_arr'] = $album_arr;
            }

        } catch (Exception $e) {
//            throw new Exception(AlbumErrMsg::ALBUM_IS_NOT_EXISTS, AlbumErrMsg::ALBUM_IS_NOT_EXISTS_NO);
        } finally {
            return $module_info;
        }
    }

    static public function getAlbumsByModule($module_list = [])
    {
        try {
            $album_id_arr = array_column($module_list['data'], 'album_id');
            $album_id_arr = array_unique($album_id_arr);

            if (count($album_id_arr) == 1)
                $user_list['data'][] = AlbumsService::getInstance()->getOne($album_id_arr[0]);
            else
                $user_list = AlbumsService::getInstance()->getList(1, count($album_id_arr), 'desc', 'id',
                    ['groupOp' => 'AND', 'rules' => [
                        ['field' => 'id', 'op' => 'in', 'data' => implode(',', $album_id_arr)]]]);

            $album_id_array = array_column($user_list['data'], 'album', 'id');

            foreach ($module_list['data'] as $idx => $info) {
                if (isset($album_id_array[$info['album_id']]) &&
                    StringOperation::isSerialized($album_id_array[$info['album_id']])) {
                    $album_arr = unserialize($album_id_array[$info['album_id']]);
                    foreach ($album_arr as $index => $album_url)
                        $album_arr[$index] = Registry::get('config')['application']['host'] . $album_url;

                    $module_list['data'][$idx]['album_arr'] = $album_arr;
                } else
                    $module_list['data'][$idx]['album_arr'] = [];
            }

        } catch (Exception $e) {
//            throw new Exception(AlbumErrMsg::ALBUM_IS_NOT_EXISTS, AlbumErrMsg::ALBUM_IS_NOT_EXISTS_NO);
        } finally {
            return $module_list;
        }
    }

    /**
     * 插入图片
     * @param array $parameters
     * @return array
     * @throws Exception
     */
    static public function createAlbum($parameters = [])
    {
        try {
            // 字符串拆分数组 并处理成序列化字符串
            $album_arr = explode(',', $parameters['album']);

            $album_serialize_str = serialize($album_arr);

            if (StringOperation::isSerialized($album_serialize_str)) {
                $album_arr = unserialize($album_serialize_str);
            } else {
                $album_arr = $album_serialize_str;
            }

            if (!is_array($album_arr) || empty($album_arr))
                throw new Exception(AlbumsException::ALBUM_ARRAY_INVALID, AlbumsException::ALBUM_ARRAY_INVALID_NO);

            foreach ($album_arr as $idx => $album)
                $album_arr[$idx] = str_replace(Registry::get('config')['application']['host'], '', $album);
            $parameters['album'] = serialize($album_arr);

            $album_res = AlbumsService::getInstance()->create(
                $parameters['type'], $parameters['album'], $parameters['stats'], $parameters['created_at'],
                $parameters['updated_at']);

//            $album_res = Member::getMemberByModule($album_res);

            return $album_res;
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * 删除相册
     * @param array $parameters
     * @throws AlbumsException
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     */
    static public function deleteAlbum($parameters = []) {
        try {
            if (! isset($parameters['id']) || empty($parameters['id']))
                throw new AlbumsException(AlbumsException::ALBUMS_ID_IS_EMPTY, AlbumsException::ALBUMS_ID_IS_EMPTY_NO);

            AlbumsService::getInstance()->delete($parameters['id']);
        } catch (AlbumsException $e) {
            throw $e;
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new AlbumsException(AlbumsException::ALBUM_DELETE_FAILURE, AlbumsException::ALBUM_DELETE_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        }
    }

}
